package com.yanchangapp.widget.calendar;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.haibin.calendarview.CalendarLayout;

/**
 * Created by apple on 2019-11-07.
 * description:滑动事件....
 */
public class MySwipeRefreshLayout extends SwipeRefreshLayout {

    private float mStartX;
    private float mStartY;
    private CalendarLayout mCalendarLayout;
    private View mContentView;
    private int mCalendarHeight;


    public void setCalendarLayout(CalendarLayout calendarLayout) {
        mCalendarLayout = calendarLayout;
    }
    public void setContentView(View contentView) {
        mContentView = contentView;
    }

    public MySwipeRefreshLayout(Context context) {
        this(context,null);
    }



    public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCalendarHeight = dpToPx(56);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mStartX = ev.getRawX();
                mStartY = ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float endY = ev.getRawY();
                float endX = ev.getRawX();
                float x = endX - mStartX;
                float y = endY - mStartY;
                /* 上下滑动拦截*/
                Log.e("滑动", "onInterceptTouchEvent: "+ mContentView.getTranslationY());
                if (y > 0){
                    if (mContentView.getTranslationY()
                            == 0 && mContentView.getScrollY() == 0){
                        // 并且rv位于顶部...
                        return true;
                    }else {
                        return false;
                    }
                }
                break;

        }
        return super.onInterceptTouchEvent(ev);
    }


    private int dpToPx(int dp){
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        return (int)(metrics.density * dp + 0.5);
    }
}
